import os
import sys
import time
import argparse
import base64
import random
sys.path.append(os.getcwd())

import svgwrite
import json
from PIL import Image

from utils.font_size import *
from utils.image import *
from utils.platform import *
from utils.io import *
from components.text import *
from components.rect import *
from components.factory import *
from settings import *

assert is_windows() or is_darwin()

def gen_distance(data, theme_type):
    safe_area_size = 20 * SCALE
    min_width = 300 * SCALE
    min_horizontal_space = 50 * SCALE
    vertical_space = 20 * SCALE
    text_size = 40 * SCALE

    theme_color = THEME_MAP.get(theme_type) or COLOR_GREEN

    data_file_name = 'distance_{}_{}'.format(time.strftime('%Y%m%d%H%M%S'), random.randint(0, 9999))
    svg_file_path = './output/{}.svg'.format(data_file_name)
    png_file_path = './output/{}.png'.format(data_file_name)

    line_heights = []
    loc_lines = []
    dis_lines = []

    content_width = min_width
    for line in data:
        dis_ele = TextAComponent(line.get('distance'), COLOR_WHITE, text_size)
        item_type = line.get('type')
        item_info = line.get('info')

        factory = ComponentFactory(text_size, COLOR_WHITE)
        ele = factory.component(item_type, item_info)
        
        loc_lines.append(ele)
        dis_lines.append(dis_ele)
        line_heights.append(max(ele.height(), dis_ele.height()))
        content_width = max(content_width, ele.width() + min_horizontal_space + dis_ele.width() + 2 * safe_area_size)

    content_height = sum(line_heights) + (len(line_heights) - 1) * vertical_space + 2 * safe_area_size

    dwg = svgwrite.Drawing(svg_file_path, profile='tiny', width=content_width, height=content_height)

    bg_rect = DoubleStrokeRectComponent(width=content_width, height=content_height, main_color=theme_color, stroke_color=COLOR_WHITE, stroke_width=4 * SCALE, r=16 * SCALE)
    bg_rect.draw(dwg, 0, 0)

    current_line_y = safe_area_size
    for (loc, dis, height) in zip(loc_lines, dis_lines, line_heights):
        unit_ele = TextAComponent('km', COLOR_WHITE, text_size * 0.6)

        loc_x = safe_area_size
        loc_y = current_line_y + (height - loc.height()) / 2

        unit_x = content_width - safe_area_size - unit_ele.width()
        dis_x = unit_x - dis.width()
        dis_y = current_line_y + (height - dis.height()) / 2
        unit_y = dis_y + (dis.height() - unit_ele.height()) * 0.8
        
        loc.draw(dwg, loc_x, loc_y)
        dis.draw(dwg, dis_x, dis_y)
        unit_ele.draw(dwg, unit_x, unit_y)
        
        current_line_y += (height + vertical_space)

    svg2png(dwg, content_width, content_height, svg_file_path, png_file_path)

    dst = Image.open(png_file_path)
    save_image(dst, png_file_path)
    with open(png_file_path, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
    return encoded_string, png_file_path


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--data', dest='Data', type=str, default='./example/distance.json', help='Path of json data file.')
    parser.add_argument('-c', '--color', dest='Color', type=str, default='g', help='Theme color.')
    args = parser.parse_args()
    data_file_path = args.Data
    theme_type = args.Color
    with open(data_file_path, 'r', encoding='utf8') as file:
        data = json.load(file)
    gen_distance(data, theme_type)